package com.haoze.nurse.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.common.utils.DateUtil;
import com.haoze.common.utils.UUIDUtil;
import com.haoze.nurse.dao.*;
import com.haoze.nurse.dto.Element;
import com.haoze.nurse.enumeration.InOutValueTypeEnum;
import com.haoze.nurse.enumeration.NurseFileTypeEnum;
import com.haoze.nurse.enumeration.StateEnum;
import com.haoze.nurse.model.*;
import com.haoze.nurse.service.NurseOrderService;
import com.haoze.nurse.service.NurseStatisticsService;
import com.haoze.nurse.vo.NurseFileData;
import com.haoze.nurse.vo.NurseFileDataDetailMarkVo;
import com.haoze.nurse.vo.NurseIntokeStokeTime;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Service
@Transactional(rollbackFor = Exception.class)
public class NurseStatisticsServiceImpl implements NurseStatisticsService {

    private static final Logger logger = LoggerFactory.getLogger(NurseStatisticsServiceImpl.class);
    @Resource
    NurseOrdersMapper nurseOrdersMapper;
    @Resource
    NurseStatisticsMapper nurseStatisticsMapper;
    @Resource
    NurseFileDataMapper nurseFileDataMapper;
    @Resource
    NurseFileDataDetailMapper nurseFileDataDetailMapper;
    @Resource
    NurseFileMapper nurseFileMapper;
    @Resource
    NurseStatisticsDetailMapper nurseStatisticsDetailMapper;
    @Resource
    NurseFileIndexMapper nurseFileIndexMapper;
    @Resource
    NurseFileDataDetailMarkMapper nurseFileDataDetailMarkMapper;
    @Resource
    NurseModifyLocationMapper nurseModifyLocationMapper;

    @Resource
    NurseOrderService nurseOrderService;

    @Override
    public Result createNewStatistics(String emrFileId) {
        return null;
    }

    @Override
    public Result doNewStatistics(String inpatNum, String emrFileId, String type, String selectDate) {
        return null;
    }


    @Override
    public String inputOutputSummary(String inpatNum, Date nodeTime, String statisticsId) {
        // 时间为空返回空
        if (nodeTime == null)
            return null;
        // sumDay暂时不用，后续用来传递选择进行总结的日期
        // 获取当前7点时间
//        // 获取固定的7点时刻，用于作为统计节点和判断获取7点之前的最近一条医嘱
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.HOUR_OF_DAY, 17);//控制时
//        cal.set(Calendar.MINUTE, 0);//控制分
//        cal.set(Calendar.SECOND, 0);//控制秒
//        Date today7Clock = cal.getTime();

        // 根据时间获取未停的需要24小时总结的医嘱
        // 获取7点之前最近一条统计记录
        long t0 = System.currentTimeMillis();
        NurseIntokeStokeTime nurseIntokeStokeTime = computeInputOutputTime(inpatNum, nodeTime, InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue());
        long t1 = System.currentTimeMillis();
        logger.info("获取统计时间范围耗时： {} ms", t1 - t0);

        if (nurseIntokeStokeTime == null)
            return null;

        // 创建统计记录
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();
        nurseStatisticsEntity.setStatisticsId(statisticsId == null ? UUIDUtil.randomString() : statisticsId);
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(nurseIntokeStokeTime.getdValue());
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics(nurseIntokeStokeTime.getFirst());
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue());
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(nurseIntokeStokeTime.getBeginDate()));
        nurseStatisticsEntity.setEndTime(sdf.format(nurseIntokeStokeTime.getEndDate()));

        List<Map<String, String>> list = nurseFileDataDetailMapper.queryStatisticsDetailDatas(inpatNum, InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue(), nurseIntokeStokeTime.getBeginDate(), nurseIntokeStokeTime.getEndDate());

        String statisticsText = countStatisticsSummary(nurseStatisticsEntity.getStatisticsId(), inpatNum, list, nurseIntokeStokeTime);
        nurseStatisticsEntity.setStatisticsText(statisticsText);
        // 插入新的统计记录
        nurseStatisticsMapper.insert(nurseStatisticsEntity);
        return nurseStatisticsEntity.getStatisticsId();
    }

    @Override
    public String inputOutputNormal(String inpatNum, Date nodeTime, String statisticsId, String statisticsType) {
        // 时间为空返回空
        if (nodeTime == null)
            return null;
        // sumDay暂时不用，后续用来传递选择进行总结的日期
        // 获取当前7点时间
//        // 获取固定的7点时刻，用于作为统计节点和判断获取7点之前的最近一条医嘱
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.HOUR_OF_DAY, 17);//控制时
//        cal.set(Calendar.MINUTE, 0);//控制分
//        cal.set(Calendar.SECOND, 0);//控制秒
//        Date today7Clock = cal.getTime();

        // 根据时间获取未停的需要24小时总结的医嘱
        // 获取7点之前最近一条统计记录
        NurseIntokeStokeTime nurseIntokeStokeTime = computeInputOutputTime(inpatNum, nodeTime, statisticsType);

        if (nurseIntokeStokeTime == null)
            return null;

        // 创建统计记录
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();
        nurseStatisticsEntity.setStatisticsId(statisticsId == null ? UUIDUtil.randomString() : statisticsId);
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(nurseIntokeStokeTime.getdValue());
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics(nurseIntokeStokeTime.getFirst());
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(statisticsType);
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(nurseIntokeStokeTime.getBeginDate()));
        nurseStatisticsEntity.setEndTime(sdf.format(nurseIntokeStokeTime.getEndDate()));

        List<Map<String, String>> list = nurseFileDataDetailMapper.queryStatisticsDetailDatas(inpatNum, statisticsType, nurseIntokeStokeTime.getBeginDate(), nurseIntokeStokeTime.getEndDate());

        String statisticsText = countStatisticsNormal(nurseStatisticsEntity.getStatisticsId(), inpatNum, list, nurseIntokeStokeTime);
        nurseStatisticsEntity.setStatisticsText(statisticsText);
        // 插入新的统计记录
        nurseStatisticsMapper.insert(nurseStatisticsEntity);
        return nurseStatisticsEntity.getStatisticsId();
    }

    /**
     * 计算获取统计起始时间和统计时长
     */
    private NurseIntokeStokeTime computeInputOutputTime(String inpatNum, Date nodeTime, String statisticsType) {
        NurseIntokeStokeTime nurseIntokeStokeTime = new NurseIntokeStokeTime();

// 1.获取当前病人最近两条医嘱，如果最近一条的开始时间和上一条的结束时间分钟级别相同，
        // 那么以上一条的医嘱记录作为起始时间，当前时间 - 医嘱时间 = 统计时长
        // 先查询两条医嘱，如果两条医嘱都已经停止
        // 查询最近两条医嘱
        // 先获取该患者最近一条未停止医嘱
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startDate = new Date();
        Map<String, String> currPatientOrder = nurseOrdersMapper.queryCurrAboutOrder(inpatNum, nodeTime, statisticsType);
        if (currPatientOrder != null) {
            try {
                startDate = sdf.parse(currPatientOrder.get("beginTime"));
                // 获取该患者与最近一条未停止医嘱上一条医嘱并且上一条停止时间和最近一条的开立时间相同的记录，如果有，把上一条医嘱的开立时间作为起始时间
                String beforeOrder = nurseOrdersMapper.queryLastAboutOrder(inpatNum, startDate, statisticsType, currPatientOrder.get("ordersItem"));
                if (beforeOrder != null) {
                    String beginDate = beforeOrder;
                    do {
                        beginDate = nurseOrdersMapper.queryLastAboutOrder(inpatNum, sdf.parse(beginDate), statisticsType, currPatientOrder.get("ordersItem"));
                        if (beginDate!=null) {
                            beforeOrder = beginDate;
                        }
                    } while (beginDate!=null);

                    startDate = sdf.parse(beforeOrder);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            return null;
        }

        // 用来标识是否是医嘱后第一次记录 0第一次，1非第一次
        String first = "0";

        // 1、查询出入量统计表当前患者最近一次2小时出入量记录
        NurseStatisticsEntity nurseStatisticsEntity = nurseStatisticsMapper.queryLastStatisticsEntity(inpatNum, statisticsType, nodeTime);

        if (nurseStatisticsEntity != null) {

            Date lastRecordDate = new Date();
            try {
                lastRecordDate = sdf.parse(nurseStatisticsEntity.getEndTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            // 比较最近记录与医嘱，将最近时间作为起始时间
            if (lastRecordDate.getTime() > startDate.getTime()) {
                // 重新根据新的起止时间计算时长
                first = "1";
                nurseIntokeStokeTime = nurseStatisticsToPatientDateDvalue(lastRecordDate, nodeTime, first);
                logger.info("获取的最近记录日期：{}", nurseStatisticsEntity.getEndTime());
            } else {
                first = "0";
                nurseIntokeStokeTime = nurseStatisticsToPatientDateDvalue(startDate, nodeTime, first);
            }
        } else {
            first = "0";
            nurseIntokeStokeTime = nurseStatisticsToPatientDateDvalue(startDate, nodeTime, first);
        }

        return nurseIntokeStokeTime;
    }

    // 统计时间距离医嘱开立时间差值
    private NurseIntokeStokeTime nurseStatisticsToPatientDateDvalue(Date startTime, Date currDate, String first) {
        NurseIntokeStokeTime nurseIntokeStokeTime = new NurseIntokeStokeTime();

        long totalMinute = (currDate.getTime() - startTime.getTime()) / 1000;
        long hour = totalMinute / 60 / 60;
        long minute = totalMinute / 60 % 60;
        Float tempHour = 0.0f;
        if (minute < 15) {
            tempHour = 0.0f;
        } else if (minute >= 15 && minute <= 45) {
            tempHour = 0.5f;
        } else {
            tempHour = 1.0f;
        }

        Float totalHour = hour + tempHour;

        nurseIntokeStokeTime.setBeginDate(startTime);
        nurseIntokeStokeTime.setEndDate(currDate);
        nurseIntokeStokeTime.setFirst(first);

        nurseIntokeStokeTime.setdValue(String.valueOf(totalHour));

        return nurseIntokeStokeTime;

    }

    // 计算出入量总结并返回节点
    private String countStatisticsSummary(String statisticsId, String inpatNum, List<Map<String, String>> list, NurseIntokeStokeTime nurseIntokeStokeTime) {
        // 计算出结果
        float inputValue = 0.0f;
        float outputValue = 0.0f;

        if (list != null && list.size() > 0) {
            for (Map<String, String> map : list) {
                if (map.get("varCode").equals("hlrlz")) {
                    inputValue += Float.parseFloat(map.get("dataValue"));
                } else {
                    outputValue += Float.parseFloat(map.get("dataValue"));
                }
            }
        }

        String inputValueStr = transStatisticsValue(inputValue);
        String outputValueStr = transStatisticsValue(outputValue);

        NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        inStatisticsDetailEntity.setDataVersion(1);
        inStatisticsDetailEntity.setDataText("总入量");
        inStatisticsDetailEntity.setDataValue(inputValueStr);
        inStatisticsDetailEntity.setFlag("0");
        inStatisticsDetailEntity.setHospitalId("");
        inStatisticsDetailEntity.setInoutType("0");
        inStatisticsDetailEntity.setSortNo("0");
        inStatisticsDetailEntity.setStatisticsId(statisticsId);
        inStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

        NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        outStatisticsDetailEntity.setDataVersion(1);
        outStatisticsDetailEntity.setDataText("总出量");
        outStatisticsDetailEntity.setDataValue(outputValueStr);
        outStatisticsDetailEntity.setFlag("0");
        outStatisticsDetailEntity.setHospitalId("");
        outStatisticsDetailEntity.setInoutType("1");
        outStatisticsDetailEntity.setSortNo("0");
        outStatisticsDetailEntity.setStatisticsId(statisticsId);
        outStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

        String statisticsText = "";
        if (nurseIntokeStokeTime.getFirst().equals("0")) {
            // 第一次统计
            statisticsText = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        } else {
            statisticsText = "24" + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        }
        return statisticsText;

    }

    // 计算出入量总结并返回节点
    private String countStatisticsNormal(String statisticsId, String inpatNum, List<Map<String, String>> list, NurseIntokeStokeTime nurseIntokeStokeTime) {
        // 计算出结果
        float inputValue = 0.0f;
        float outputValue = 0.0f;

        if (list != null && list.size() > 0) {
            for (Map<String, String> map : list) {
                if (map.get("varCode").equals("hlrlz")) {
                    inputValue += Float.parseFloat(map.get("dataValue"));
                } else {
                    outputValue += Float.parseFloat(map.get("dataValue"));
                }
            }
        }

        String inputValueStr = transStatisticsValue(inputValue);
        String outputValueStr = transStatisticsValue(outputValue);

        NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        inStatisticsDetailEntity.setDataVersion(1);
        inStatisticsDetailEntity.setDataText("总入量");
        inStatisticsDetailEntity.setDataValue(inputValueStr);
        inStatisticsDetailEntity.setFlag("0");
        inStatisticsDetailEntity.setHospitalId("");
        inStatisticsDetailEntity.setInoutType("0");
        inStatisticsDetailEntity.setSortNo("0");
        inStatisticsDetailEntity.setStatisticsId(statisticsId);
        inStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

        NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        outStatisticsDetailEntity.setDataVersion(1);
        outStatisticsDetailEntity.setDataText("总出量");
        outStatisticsDetailEntity.setDataValue(outputValueStr);
        outStatisticsDetailEntity.setFlag("0");
        outStatisticsDetailEntity.setHospitalId("");
        outStatisticsDetailEntity.setInoutType("1");
        outStatisticsDetailEntity.setSortNo("0");
        outStatisticsDetailEntity.setStatisticsId(statisticsId);
        outStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

        String statisticsText = "";
        if (nurseIntokeStokeTime.getFirst().equals("0")) {
            // 第一次统计
            statisticsText = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "-小" + "-时" + "-入" + "-量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        } else {
            statisticsText = "24" + "-小" + "-时" + "-入" + "-量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        }
        return statisticsText;

    }

    public String transStatisticsValue(Float value) {
        String valueStr = String.valueOf(value);
        // 判断第二位小数不为空
        Pattern pattern1 = Pattern.compile("(\\d*\\.[0-9][1-9])");
        // 判断第一位小数不为空
        Pattern pattern2 = Pattern.compile("(\\d*\\.[1-9])");
        if (pattern1.matcher(valueStr).matches()) {
            // 是两位小数
            return valueStr;
        } else if (pattern2.matcher(valueStr).matches()) {
            // 是一位小数
            String[] strings = valueStr.split("\\.");
            return strings[0] + "." + strings[1].substring(0, 1);
        } else {
            // 是整数
            String[] strings = valueStr.split("\\.");
            return strings[0];
        }

    }

    @Override
    public void updateNeedResetStatistics(String inpatNum, Date nodeTime, String statisticsType,String recordType) {
        // 1查找需要重新统计的记录
        List<NurseStatisticsEntity> list = nurseStatisticsMapper.queryNeedUpdateStatistics(inpatNum, nodeTime, statisticsType);
        // 2重新统计记录
        if (ArrayUtils.isNotEmpty(list.toArray())) {
            for (NurseStatisticsEntity entity : list) {
                // 删除原统计记录
                nurseStatisticsMapper.deleteStatistics(entity.getStatisticsId());
                // 保持统计ID不变（保证引用统计的列表记录可以正常引用统计记录），创建新的统计记录
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date nodeDate = null;
                try {
                    nodeDate = sdf.parse(entity.getEndTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                // 类型为出入量总结
                if (entity.getStatisticsType().equals(InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue())) {
                    inputOutputSummary(inpatNum, nodeDate, entity.getStatisticsId());
                } else {
//                    inputOutputNormal(inpatNum, nodeDate, entity.getStatisticsId(), statisticsType);
                    inputOutputStatistics(inpatNum, entity.getEndTime(), entity.getStatisticsType(), entity.getStatisticsId(),recordType);
                }
            }
        }

    }

    @Override
    public Result doStatistics(String type, String recordTime, String inpatNum) {
        // 判断是否存在医嘱
        Map<String, String> currPatientOrder = nurseOrdersMapper.queryCurrAboutOrder(inpatNum, DateUtil.stringToDate(recordTime), type);
        if (currPatientOrder == null)
            return ResultGenerator.genOkResult("当前患者不存在当前类型医嘱");
        // 返回文本数据元类型节点
        NurseFileData fileData = new NurseFileData();
        fileData.setEmrFileDataList(new ArrayList<>());

        Element element = new Element();
        String elementId = nurseFileMapper.querySysElementId(StateEnum.MB_WB.getValue());
        if (org.apache.commons.lang3.StringUtils.isBlank(elementId)) {
            return ResultGenerator.genOkResult("未找到文本数据元");
        } else {
            element.setId(elementId);
            element.setType("1");
            element.setName("文本数据元");
        }
        fileData.setElement(element);

        NurseFileDataDetailEntity nurseFileDataDetailEntity = new NurseFileDataDetailEntity();
        nurseFileDataDetailEntity.setEditIdentification("1"); // 不可编辑
        nurseFileDataDetailEntity.setShowIdentification("0"); // 显示
        nurseFileDataDetailEntity.setPrintIdentification("0"); // 打印
        nurseFileDataDetailEntity.setDeleteIdentification("0"); // 可删除
        nurseFileDataDetailEntity.setElementType("1");
        nurseFileDataDetailEntity.setElementId(elementId);
        nurseFileDataDetailEntity.setQuoteType("4"); // 统计的引用类型为4
        nurseFileDataDetailEntity.setQuoteIdentify(InOutValueTypeEnum.fromValue(type).getEnumValue());
        nurseFileDataDetailEntity.setDataCode("");
        nurseFileDataDetailEntity.setDataValue("【记" + InOutValueTypeEnum.fromValue(type).getEnumName() + "】");

        fileData.setFileIndexDataEntity(nurseFileDataDetailEntity);
        return ResultGenerator.genOkResult(fileData);
    }

    @Override
    public String inputOutputStatistics(String inpatNum, String recordTime, String statisticsType, String statisticsId,String recordType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date nodeDate = null;
        try {
            nodeDate = sdf.parse(recordTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long t0 = System.currentTimeMillis();
        NurseIntokeStokeTime nurseIntokeStokeTime = computeInputOutputTime(inpatNum, nodeDate, statisticsType);
        long t1 = System.currentTimeMillis();
        logger.info("获取统计时间范围耗时： {} ms", t1 - t0);

        if (nurseIntokeStokeTime == null)
            return null;

        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();
        nurseStatisticsEntity.setStatisticsId(statisticsId == null ? UUIDUtil.randomString() : statisticsId);
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(nurseIntokeStokeTime.getdValue());
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics(nurseIntokeStokeTime.getFirst());
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(statisticsType);
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(nurseIntokeStokeTime.getBeginDate()));
        nurseStatisticsEntity.setEndTime(sdf.format(nurseIntokeStokeTime.getEndDate()));

        List<Map<String, String>> list = nurseFileDataDetailMapper.queryStatisticsDetailDatas(inpatNum, statisticsType, nurseIntokeStokeTime.getBeginDate(), nurseIntokeStokeTime.getEndDate());

        String statisticsText = countStatistics(nurseStatisticsEntity.getStatisticsId(), inpatNum, list, nurseIntokeStokeTime, statisticsType,recordType);

        nurseStatisticsEntity.setStatisticsText(statisticsText);
        // 插入新的统计记录
        nurseStatisticsMapper.insert(nurseStatisticsEntity);
        return nurseStatisticsEntity.getStatisticsId();
    }


    // 计算出入量总结并返回节点
    private String countStatistics(String statisticsId, String inpatNum, List<Map<String, String>> list, NurseIntokeStokeTime nurseIntokeStokeTime, String statisticsType,String recordType) {

        // 计算出结果
        if (statisticsType.equals(InOutValueTypeEnum.IN_OUT_2HOURS.getEnumValue())) {
            // 2小时出入量计算
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "2";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue()));
                //dValue = nurseIntokeStokeTime.getdValue();
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsInput(inpatNum, list,recordType);

                statictisOutMap = countStatisticsOutput(inpatNum, list,recordType);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = "患者"+dValue+"小时入量"+totalInValue+"ml，"+qizhong+inMiddleText;
                statisticsText = "" + dValue + "小时入量" + transStatisticsValue(totalInValue) + "ml，" + qizhong + inMiddleText;

                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                statisticsText = statisticsText + dValue + "小时出量" + transStatisticsValue(totalOutValue) + "ml，" + qizhong + outMiddleText;
                statisticsText = statisticsText.substring(0, statisticsText.length());  //-1
                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {

                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者2小时入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，2小时出量**m l
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";

                } else {
                    //statisticsText = "患者2小时入量0ml，2小时出量0ml。";
                    statisticsText = "2小时入量0ml，2小时出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.IN_OUT_8HOURS.getEnumValue())) {
            // 本班出入量计算
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                // dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
                // dValue = nurseIntokeStokeTime.getdValue() + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsInput(inpatNum, list,recordType);
                statictisOutMap = countStatisticsOutput(inpatNum, list,recordType);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = "患者"+dValue+"入量"+totalInValue+"ml，"+qizhong+inMiddleText;
                statisticsText = "" + dValue + "入量" + transStatisticsValue(totalInValue) + "ml，" + qizhong + inMiddleText;

                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                statisticsText = statisticsText + dValue + "出量" + transStatisticsValue(totalOutValue) + "ml，" + qizhong + outMiddleText;
                statisticsText = statisticsText.substring(0, statisticsText.length()); //-1
                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者本班入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，本班出量**m l
                    // statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时入量0ml，" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时出量0ml。";
                    statisticsText = "" + dValue + "入量0ml，" + dValue + "出量0ml。";
                } else {
                    //statisticsText = "患者本班入量0ml，本班出量0ml。";
                    statisticsText = "本班入量0ml，本班出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.OUT_8HOURS.getEnumValue())) {
            // 本班出量计算
//            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
//            String inMiddleText = "";
            String outMiddleText = "";
//            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                //dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue()))+ "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
//                statictisInMap = countStatisticsInput(inpatNum, list);
                statictisOutMap = countStatisticsOutput(inpatNum, list,recordType);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                // statisticsText = "患者" + dValue+"出量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "出量" + transStatisticsValue(totalOutValue) + "ml，" + qizhong + outMiddleText + "。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时出量0ml。";
                    statisticsText = "" + dValue + "出量0ml。";
                } else {
                    //statisticsText = "患者本班出量0ml。";
                    statisticsText = "本班出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.OUT_24HOURS.getEnumValue())) {
            // 24小时出量计算
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String outMiddleText = "";
            float totalOutValue = 0;
            String dValue = "24小时";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
//                statictisInMap = countStatisticsInput(inpatNum, list);
                statictisOutMap = countStatisticsOutput(inpatNum, list,recordType);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                // statisticsText = "患者" + dValue+"出量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                // statisticsText = "" + dValue+"出量"+ transStatisticsValue(totalOutValue) +"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "出量" + transStatisticsValue(totalOutValue) + "ml。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时出量0ml。";
                } else {
                    //statisticsText = "患者24小时出量0ml。";
                    statisticsText = "24小时出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.URINE_OUT_8HOURS.getEnumValue())) {
            // 本班尿量
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String outMiddleText = "";
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                //dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisOutMap = countStatisticsUrineOutput(inpatNum, list);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                //statisticsText = "患者" + dValue+"尿量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "尿量" + transStatisticsValue(totalOutValue) + "ml。"; //+qizhong+outMiddleText + "。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时尿量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时尿量0ml。";
                    statisticsText = "" + dValue + "尿量0ml。";
                } else {
                    // statisticsText = "患者本班尿量0ml。";
                    statisticsText = "本班尿量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.URINE_OUT_24HOURS.getEnumValue())) {
            // 24小时尿量 [24小时出量计算]
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String outMiddleText = "";
            float totalOutValue = 0;
            String dValue = "24";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisOutMap = countStatisticsUrineOutput(inpatNum, list);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                //statisticsText = "患者" + dValue+"小时尿量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "小时尿量" + transStatisticsValue(totalOutValue) + "ml。"; //+qizhong+outMiddleText + "。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时尿量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时尿量0ml。";
                } else {
                    //statisticsText = "患者24小时尿量0ml。";
                    statisticsText = "24小时尿量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.WASH_IN_OUT_8HOURS.getEnumValue())) {
            // 本班冲洗出入量计算
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                //dValue =  transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsWashInOutIntake(inpatNum, list);
                statictisOutMap = countStatisticsWashInOutOutput(inpatNum, list);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                // statisticsText = "患者"+dValue+"冲洗入量"+totalInValue+"ml，"; //+qizhong+inMiddleText;
                statisticsText = "" + dValue + "冲洗入量" + transStatisticsValue(totalInValue) + "ml，"; //+qizhong+inMiddleText;

                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = statisticsText +dValue+"冲洗出量"+transStatisticsValue(totalOutValue)+"ml，";//+qizhong+outMiddleText;
                statisticsText = statisticsText + "冲洗出量" + transStatisticsValue(totalOutValue) + "ml，";
                statisticsText = statisticsText.substring(0, statisticsText.length() - 1);

                if (totalInValue > totalOutValue) {
                    // statisticsText += statisticsText + "，其中入超" + (totalInValue-totalOutValue) + "ml";
                    statisticsText = statisticsText + "，入超" + transStatisticsValue((totalInValue - totalOutValue)) + "ml";
                } else if (totalInValue < totalOutValue) {
                    // statisticsText += statisticsText + "，其中出超" + (totalOutValue-totalInValue) + "ml";
                    statisticsText = statisticsText + "，出超" + transStatisticsValue((totalOutValue - totalInValue)) + "ml";
                }

                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {

                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者本班入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，本班出量**m l
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时冲洗入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时冲洗出量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗入量0ml，" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗出量0ml。";
                    statisticsText = "" + dValue + "冲洗入量0ml，" + dValue + "冲洗出量0ml。";
                } else {
                    // statisticsText = "患者本班冲洗入量0ml，本班冲洗出量0ml。";
                    statisticsText = "本班冲洗入量0ml，本班冲洗出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.WASH_IN_OUT_24HOURS.getEnumValue())) {
            // 24小时冲洗出入量
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "24小时";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                // dValue = nurseIntokeStokeTime.getdValue() + "小时";
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsWashInOutIntake(inpatNum, list);
                statictisOutMap = countStatisticsWashInOutOutput(inpatNum, list);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    //qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                // statisticsText = "患者"+dValue+"冲洗入量"+totalInValue+"ml，"+qizhong+inMiddleText;
                statisticsText = "" + dValue + "冲洗入量" + transStatisticsValue(totalInValue) + "ml，"; //+qizhong+inMiddleText;


                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    //qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = statisticsText +dValue+"冲洗出量"+transStatisticsValue(totalOutValue)+"ml，"; //+qizhong+outMiddleText;
                statisticsText = statisticsText + "冲洗出量" + transStatisticsValue(totalOutValue) + "ml，";
                statisticsText = statisticsText.substring(0, statisticsText.length() - 1);

                if (totalInValue > totalOutValue) {
                    statisticsText = statisticsText + "，入超" + transStatisticsValue((totalInValue - totalOutValue)) + "ml";
                } else if (totalInValue < totalOutValue) {
                    statisticsText = statisticsText + "，出超" + transStatisticsValue((totalOutValue - totalInValue)) + "ml";
                }

                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {

                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者本班入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，本班出量**m l
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时冲洗入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时冲洗出量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗入量0ml，" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗出量0ml。";
                } else {
                    //statisticsText = "患者24小时冲洗入量0ml，24小时冲洗出量0ml。";
                    statisticsText = "24小时冲洗入量0ml，24小时冲洗出量0ml。";
                }
                return statisticsText;
            }
        } else {
            return "其他统计类型";
        }

    }


    // 计算入量
    private Map<String, Float> countStatisticsInput(String inpatNum, List<Map<String, String>> list,String recordType) {

        // 获取当前患者的个人出量名称
        List<Map<String, String>> outputList = getOutputList(inpatNum,recordType);

        //获取选中的医嘱补液键值
        //List<Map<String, String>>  allMapBy = nurseOrderService.getAddByMap();

        Map<String, Float> statictisInMap = new HashMap<>();
        statictisInMap.put("进食", 0.0f);
        statictisInMap.put("引流液", 0.0f);
        statictisInMap.put("补液", 0.0f);
        statictisInMap.put("其他", 0.0f);

//        //累计添加补液统计值
//        for (Map<String, String> m : byList) {
//            Float v = statictisInMap.get("补液");
//            v = v + Float.parseFloat(m.get("dataValue")); // 累加
//            statictisInMap.put("补液", v);
//        }

        // List fluids = nurseFileMapper.queryFluids(); 因为用法在使用时会进行转换，所以不能直接使用补液字典进行匹配
        List fluids = new ArrayList();
        fluids.add("ivgtt");
        fluids.add("IV微量泵维持");
        fluids.add("皮内注射");
        fluids.add("H");
        fluids.add("IM");
        fluids.add("IV");
        fluids.add("ivgtt带手术室");
        fluids.add("加入ivgtt液");


        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlrlz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");
                boolean contain = false;
                for (Map<String, String> map2 : outputList) {
                    String key = map2.get("dataText");
                    if (title.contains(key)) {
                        Float v = statictisInMap.get("引流液");
                        v = v + Float.parseFloat(value);
                        statictisInMap.put("引流液", v);
                        contain = true;
                        break;
                    }
                }

                if (!contain) {
                    // 判断统计map是否包含获取到的dataValue
                    if (statictisInMap.get(title) != null) {
                        // 获取统计集合里已经存在的key的值
                        Float v = statictisInMap.get(title);
                        // 累加
                        v = v + Float.parseFloat(value);
                        // 重新把值存入统计集合
                        statictisInMap.put(title, v);
                    } else {
                        if (!title.equals("")) {
                            String administration = "";
                            String expandFileds = map.get("expandFields");
                            if (StringUtils.isNotBlank(map.get("expandFields"))) {
                                Map expandFiledsMap = JSON.parseObject(expandFileds, Map.class);
                                administration = (String) expandFiledsMap.get("administration");
                            }
                            administration = administration==null?"":administration;
                            // 先判断是否可以直接把intakeStroke.getDataValue()作为统计集合的key存入统计集合
                            if (title.contains("蛋白")) { // 含蛋白类型
                                // 直接获取名称为intakeStroke.getDataValue()的值
                                Float v = Float.parseFloat(value);
                                // 把值存入统计集合
                                statictisInMap.put(title, v);
                            } else if (title.contains("鼻饲")
                                    || title.contains("空肠注入")
                                    || title.contains("混服")
                                    || title.contains("口服")
                                    || title.contains("开水冲服")
                                    || title.contains("开水泡服")
                                    || title.contains("温水冲服")
                                    || title.contains("温水泡服")
                                    || title.contains("饮水")
                                    || title.contains("肠内营养")
                                    || title.contains("进食")
                                    || administration.contains("鼻饲")
                                    || administration.contains("空肠注入")
                                    || administration.contains("混服")
                                    || administration.contains("口服")
                                    || administration.contains("开水冲服")
                                    || administration.contains("开水泡服")
                                    || administration.contains("温水冲服")
                                    || administration.contains("温水泡服")
                                    || administration.contains("饮水")
                                    || administration.contains("肠内营养")
                                    || administration.contains("进食")) { // 鼻饲类型
                                // 获取统计集合里已经存在的key的值
                                Float v = statictisInMap.get("进食");
                                // 累加
                                v = v + Float.parseFloat(value);
                                // 把值存入统计集合
                                statictisInMap.put("进食", v);
                            } else if (title.contains("ivgtt")
                                    || title.contains("补液")) {
                                // 获取统计集合里已经存在的key的值
                                Float v = statictisInMap.get("补液");
                                // 累加
                                v = v + Float.parseFloat(value);
                                // 把值存入统计集合
                                statictisInMap.put("补液", v);
                            } else {
                                // 查询是否包含在血制品中，也就是intakeStroke.getTitle中是否包含血制品字典中的某种血制品，
                                // 如果包含，直接作为血制品进行存储，如果不包含则作为其他处理
                                // 获取包含当前字符的血制品
//                            List bloodList = nurseFileIndexMapper.queryBloodDictionary(intakeStroke.getTitle());
                                Map<String, String> bloodMap = nurseFileMapper.queryBloodDatasetByName(title);

                                if (bloodMap != null && bloodMap.get("elementName") != null && bloodMap.get("elementName").length() > 0) {
                                    // 如果集合中包含血制品数据元，如：“少浆血”
                                    if (statictisInMap.containsKey(bloodMap.get("elementName"))) {
                                        // 获取统计集合里已经存在的key的值
                                        Float v = statictisInMap.get(bloodMap.get("elementName"));
                                        // 累加
                                        v = v + Float.parseFloat(value);
                                        // 把值存入统计集合
                                        statictisInMap.put(bloodMap.get("elementName"), v);
                                    } else { // 如果集合中不包含该类型，则重新设置初始值
                                        Float v = Float.parseFloat(value);
                                        // 把值存入统计集合
                                        statictisInMap.put(bloodMap.get("elementName"), v);
                                    }

                                } else {

                                    if (StringUtils.isNotBlank(administration) && fluids.contains(administration)) {
                                        Float v = statictisInMap.get("补液");
                                        v = v + Float.parseFloat(value); // 累加
                                        statictisInMap.put("补液", v);

                                    } else {
                                        Float v = statictisInMap.get("其他");
                                        v = v + Float.parseFloat(value);  //  把值存入统计集合
                                        statictisInMap.put("其他", v);
                                    }

                                }
                            }
                        } else {
                            Float v = statictisInMap.get("其他");
                            v = v + Float.parseFloat(value);
                            // 把值存入统计集合
                            statictisInMap.put("其他", v);
                        }
                    }
                }

            }
        }
        return statictisInMap;
    }

    // 计算出量
    private Map<String, Float> countStatisticsOutput(String inpatNum, List<Map<String, String>> list,String recordType) {
        // 计算出结果
        // 获取当前患者的个人出量名称
        List<Map<String, String>> outputList = getOutputList(inpatNum,recordType);

        // 所有出量类型统计集合
        Map<String, Float> statictisOutMap = new HashMap<>();
        statictisOutMap.put("大便", 0.0f);
        statictisOutMap.put("尿", 0.0f);
        statictisOutMap.put("胃内容物", 0.0f);
        statictisOutMap.put("呕血", 0.0f);
        statictisOutMap.put("呕吐物", 0.0f);
        statictisOutMap.put("引流液", 0.0f);

        statictisOutMap.put("恶露", 0.0f);
        statictisOutMap.put("阴道出血", 0.0f);
        statictisOutMap.put("泌乳", 0.0f);

        statictisOutMap.put("其他", 0.0f);

        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlclz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");

                // 先过滤尿
                if (title.contains("尿")
                        || title.contains("膀胱造瘘引流")
                        || title.contains("膀胱造瘘管")
                        || title.contains("肾造瘘管")
                        || title.contains("肾盂造瘘管")) {
                    Float v = statictisOutMap.get("尿");
                    v = v + Float.parseFloat(value);
                    statictisOutMap.put("尿", v);
                    continue;
                }

                boolean contain = false;
                for (Map<String, String> map2 : outputList) {
                    String key = map2.get("dataText");
                    if (title.contains(key)) {
                        Float v = statictisOutMap.get("引流液");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("引流液", v);
                        contain = true;
                        break;
                    }
                }

                // contain == false不包含在引流管内
                if (contain == false) {

                    if (title.contains("大便")) {
                        Float v = statictisOutMap.get("大便");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("大便", v);
                    } else if (title.contains("胃内容物")) {
                        Float v = statictisOutMap.get("胃内容物");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("胃内容物", v);
                    } else if (title.contains("呕血")) {
                        Float v = statictisOutMap.get("呕血");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("呕血", v);
                    } else if (title.contains("呕吐物")) {
                        Float v = statictisOutMap.get("呕吐物");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("呕吐物", v);
                    } else if (title.contains("恶露")) {
                        Float v = statictisOutMap.get("恶露");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("恶露", v);
                    } else if (title.contains("阴道出血")) {
                        Float v = statictisOutMap.get("阴道出血");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("阴道出血", v);
                    } else if (title.contains("泌乳")) {
                        Float v = statictisOutMap.get("泌乳");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("泌乳", v);
                    } else {
                        Float v = statictisOutMap.get("其他");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("其他", v);
                    }
                }

            }
        }
        return statictisOutMap;
    }


    // 计算尿量
    private Map<String, Float> countStatisticsUrineOutput(String inpatNum, List<Map<String, String>> list) {
        // 所有出量类型统计集合
        Map<String, Float> statictisMap = new HashMap<>();
        Float urine = list.stream().filter(map -> "hlclz".equals(map.get("varCode"))).filter(map -> {
            String title = map.get("dataText") == null ? "" : map.get("dataText");
            return (title.contains("尿")
                    || title.contains("膀胱造瘘引流")
                    || title.contains("膀胱造瘘管")
                    || title.contains("肾造瘘管")
                    || title.contains("肾盂造瘘管"));
        }).map(map -> map.get("dataValue") == null ? 0.0F : Float.parseFloat(map.get("dataValue"))).reduce(Float::sum).orElseGet(() -> 0.0F);
        statictisMap.put("尿", urine);
//        statictisMap.put("尿", 0.0F);
//        for (Map<String, String> map : list) {
//            if (map.get("varCode").equals("hlclz")) {
//                String title = map.get("dataText") == null ? "" : map.get("dataText");
//                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");
//
//                if (title.contains("尿")
//                        || title.contains("膀胱造瘘引流")
//                        || title.contains("膀胱造瘘管")
//                        || title.contains("肾造瘘管")
//                        || title.contains("肾盂造瘘管")) {
//                    Float v = statictisMap.get("尿");
//                    v = v + Float.parseFloat(value);
//                    statictisMap.put("尿", v);
//                }
//            }
//        }
        return statictisMap;
    }

    // 计算冲洗出入量
    private Map<String, Float> countStatisticsWashInOutIntake(String inpatNum, List<Map<String, String>> list) {

        Map<String, Float> statictisInMap = new HashMap<>();
        statictisInMap.put("冲洗入量", 0.0f);

        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlrlz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");

                // 获取医嘱带入的用法和嘱托
                String administration = "";
                String ordersRemark = "";
                String fullName = "";
                String expandFileds = map.get("expandFields");
                if (StringUtils.isNotBlank(expandFileds)) {
                    Map expandFiledsMap = JSON.parseObject(expandFileds, Map.class);
                    administration = (String) expandFiledsMap.get("administration");
                    ordersRemark = (String) expandFiledsMap.get("ordersRemark");
                    fullName = (String) expandFiledsMap.get("fullName");
                }
                administration = administration==null?"":administration;
                ordersRemark = ordersRemark==null?"":ordersRemark;
                fullName = fullName==null?"":fullName;

                if (title.contains("冲洗") || administration.contains("冲洗") || ordersRemark.contains("冲洗") || fullName.contains("冲洗")) {
                    Float v = statictisInMap.get("冲洗入量");
                    v = v + Float.parseFloat(value);
                    // 把值存入统计集合
                    statictisInMap.put("冲洗入量", v);
                }

            }
        }

        return statictisInMap;
    }

    // 计算冲洗出入量
    private Map<String, Float> countStatisticsWashInOutOutput(String inpatNum, List<Map<String, String>> list) {

        // 所有出量类型统计集合
        Map<String, Float> statictisOutMap = new HashMap<>();
        statictisOutMap.put("冲洗出量", 0.0f);

        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlclz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");

                if (title.contains("双套管")) {
                    Float v = statictisOutMap.get("冲洗出量");
                    v = v + Float.parseFloat(value);
                    // 把值存入统计集合
                    statictisOutMap.put("冲洗出量", v);
                }

            }

        }

        return statictisOutMap;
    }


    /**
     * 获取文书编号对应的出量名称及其扩展
     *
     * @return
     */
    private List<Map<String, String>> getOutputList(String inpatNum,String recordType) {
        String longNumStr = "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿";
        // 获取当前患者的个人出量名称
        List<Map<String, String>> outputList;
        if (NurseFileTypeEnum.Care.getEnumValue().equals(recordType)) {
            outputList = nurseFileMapper.queryOutputElementSelections();
        } else {
            //新生儿、儿科导管备注获取
            outputList = nurseFileMapper.queryCommonDictionryValues("ck_jlxsedgzd");
        }
        // 1获取EMR_FILE_INDEX中记录的NURSE_CUSTOMIZE_EXPAND获取出量名称和项目名称的扩展记录
        String recordJson = nurseFileMapper.queryExtendsByInpatNum(inpatNum, recordType);
        if (recordJson != null) {
            Map<String, List<Map<String, String>>> map1 = JSON.parseObject(recordJson, Map.class);
            int optionsSrcCount = outputList.size();
            // 出量管道
            List<Map<String, String>> pipeList = new ArrayList<>();
            if (map1.get("pipe") != null && NurseFileTypeEnum.Care.getEnumValue().equals(recordType)) {
                pipeList = map1.get("pipe");
            }else if(map1.get("newpipe") != null){
                pipeList = map1.get("newpipe");
            }
            for (int i = 0; i < pipeList.size(); i++) {
                Map<String, String> map3 = pipeList.get(i);
                String currName = map3.get("name");
                String numStr = longNumStr.substring(optionsSrcCount + i, optionsSrcCount + i + 1);
                String fullName = numStr + currName;
                Map<String, String> map2 = new HashMap<>();
                map2.put("dataText", fullName);
                map2.put("id", "");
                map2.put("dataValue", "");
                outputList.add(map2);
            }
        }
        return outputList;
    }

    @Override
    public void updateAllNeedResetStatistics(String inpatNum, Date nodeTime,String recordType) {
        // 1查找需要重新统计的记录
        List<NurseStatisticsEntity> list = nurseStatisticsMapper.queryNeedUpdateStatistics(inpatNum, nodeTime, null);
        // 2重新统计记录
        if (CollectionUtils.isNotEmpty(list)) {
            for (NurseStatisticsEntity entity : list) {
                // 删除原统计记录
                nurseStatisticsMapper.deleteStatistics(entity.getStatisticsId());
                // 保持统计ID不变（保证引用统计的列表记录可以正常引用统计记录），创建新的统计记录
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date nodeDate = null;
                try {
                    nodeDate = sdf.parse(entity.getEndTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                // 类型为出入量总结
                if (entity.getStatisticsType().equals(InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue())) {
                    long t0 = System.currentTimeMillis();
                    inputOutputSummary(inpatNum, nodeDate, entity.getStatisticsId());
                    long t1 = System.currentTimeMillis();

                    logger.info("{}出入量总结耗时： {} ms", DateUtil.dateToString(nodeDate, "yyyy-MM-dd HH:mm"), t1 - t0);
                } else {
                    long t0 = System.currentTimeMillis();
//                    inputOutputNormal(inpatNum, nodeDate, entity.getStatisticsId(), statisticsType);
                    inputOutputStatistics(inpatNum, entity.getEndTime(), entity.getStatisticsType(), entity.getStatisticsId(),recordType);
                    long t1 = System.currentTimeMillis();

                    logger.info("{}-类型{}统计耗时： {} ms", entity.getEndTime(), entity.getStatisticsType(), t1 - t0);
                }
            }
        }
    }

    /**
     * 获取所有出入量类型
     *
     * @return
     */
    @Override
    public Result getInOutValueType(String inpatNum, String recordTime) {

        InOutValueTypeEnum.putSourceEnum();
        // 移除第一个出入量总结，总结是在列表进行
        Map<String, InOutValueTypeEnum> map = InOutValueTypeEnum.enumMap;
        Map<String, InOutValueTypeEnum> tempMap = new HashMap();
        for (String key:map.keySet()) {
            if ("0".equals(key)) {
            } else {
                InOutValueTypeEnum typeEnum = (InOutValueTypeEnum) map.get(key);
                Map<String, String> currPatientOrder = nurseOrdersMapper.queryCurrAboutOrder(inpatNum, DateUtil.stringToDate(recordTime), typeEnum.getEnumValue());
                if (currPatientOrder != null) {
                    tempMap.put(key, typeEnum);
                }
            }
        }

//        Iterator iterator = map.keySet().iterator();
//        while (iterator.hasNext()) {
//            String key = (String) iterator.next();
//            if ("0".equals(key)) {
//                iterator.remove();
//                map.remove(key);
//                iterator = map.keySet().iterator();
//            } else {
//                InOutValueTypeEnum typeEnum = (InOutValueTypeEnum) map.get(key);
//                Map<String, String> currPatientOrder = nurseOrdersMapper.queryCurrAboutOrder(inpatNum, DateUtil.stringToDate(recordTime), typeEnum.getEnumValue());
//                if (currPatientOrder == null) {
//                    iterator.remove(); // TODO: 此处报错 java.util.ConcurrentModificationException: null
//                    map.remove(key);
//                    iterator = map.keySet().iterator();
//                }
//            }
//        }
        return ResultGenerator.genOkResult(tempMap);
    }

    /**
     * 根据病人入院号及时间获取实时床号
     * @param inpatNum
     * @param recordTime
     * @return
     */
    @Override
    public Result getPreBedNo(String inpatNum, String recordTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String currentTime = null;
        try {
            currentTime = sdf.format(sdf.parse(recordTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //若有换病区数据则查看传入时间最近的一条换病区信息的转入床号
        String preBedNo = null;
        String transferBedno = nurseStatisticsMapper.getBedNoByTime(inpatNum,currentTime);
        if(StringUtils.isNotBlank(transferBedno)){
            preBedNo = transferBedno;
        }else{
            //若没有转病区记录则查找当前病人表中的床号
            String patientBedNo = nurseStatisticsMapper.queryBedNoByInpatNum(inpatNum);
            if(!"0".equals(patientBedNo)){
                preBedNo = patientBedNo;
            }else{
                //若病人信息中床号为0记为出院记录，则查询最近一条护理记录单的床号
                String fileDataBedNo = nurseStatisticsMapper.getPreNurseFileDataBedNo(inpatNum,currentTime);
                if(StringUtils.isNotBlank(fileDataBedNo)){
                    preBedNo = fileDataBedNo;
                }
            }
        }
        if(StringUtils.isNotBlank(preBedNo)){
            return ResultGenerator.genOkResult(preBedNo);
        }else {
            return ResultGenerator.genFailedResult("当前病人无法获取床号");
        }
    }

    /**
     * 护理信息修改痕迹
     *
     * @return
     */
    @Override
    public Result modifyRecord(Integer page, Integer pageSize, String inpatNum, String recordType, String createTime, String recordTime, String userName, String createUserName, String modifyLocation, String modifyType) {
        String userId = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("zuul_id");
        NurseModifyLocationEntity entity = new NurseModifyLocationEntity();
        entity.initAdd();
        entity.setModifyLocation(modifyLocation);
        entity.setUserId(userId);
        entity.setRecordType(recordType);
        nurseModifyLocationMapper.insertOrUpdate(entity);
        PageHelper.startPage(page, pageSize);
        List<NurseFileDataDetailMarkVo> list = nurseFileDataDetailMarkMapper.queryModifyRecord(inpatNum, recordType, createTime, recordTime, userName, createUserName, modifyLocation, modifyType);
        PageInfo<NurseFileDataDetailMarkVo> pageInfo = new PageInfo<>(list);
        return ResultGenerator.genOkResult(pageInfo);
    }

}
